﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.businesslogic;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using umbraco.NodeFactory;
using System.Text.RegularExpressions;
using umbraco.cms.helpers;

public partial class usercontrols_TeamViewControl_CommentControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["comment"] == "open")
        {
            this.CommentTextBox.Visible = true;
            this.SubmitButton.Visible = true;
        }
    }
    protected void CreateCommentDocument(string comment)
    {
        Document document = new Document(Node.GetCurrent().Id);
        Document commentDocument = Document.MakeNew("comment", DocumentType.GetByAlias("TextPage"), document.Creator, document.Id);
        commentDocument.getProperty("textpagebody").Value = comment;
        commentDocument.Publish(document.Creator);
        umbraco.library.UpdateDocumentCache(commentDocument.Id);
        umbraco.library.RefreshContent();
    }
    protected void CommentButton_Click(object sender, EventArgs e)
    {
        this.CommentTextBox.Visible = !this.CommentTextBox.Visible;
        this.SubmitButton.Visible = this.CommentTextBox.Visible;
        Node node = Node.GetCurrent();
        string urlName = url.FormatUrl(node.Name.ToLower());
        Response.Redirect("/blog/" + urlName + ".aspx?comment=open#" + CommentButton.ClientID);
    }
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        if (CommentTextBox.Text.ToString() != "")
            CreateCommentDocument(this.CommentTextBox.Text.ToString());
        this.CommentTextBox.Text = string.Empty;
        this.CommentTextBox.Visible = !this.CommentTextBox.Visible;
        this.SubmitButton.Visible = this.CommentTextBox.Visible;
        Node node = Node.GetCurrent();
        string urlName = url.FormatUrl(node.Name.ToLower());
        Response.Redirect("/blog/" + urlName + ".aspx#" + CommentButton.ClientID);
    }
}